﻿using Common.Utils;
using Entity.Enums;
using Entity.Models.dto;
using Entity.Models.Entities;
using Entity.Models.vo;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RepositorySys.Filte;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using utils;
using utils.enums;
using utils.EX;
using Utils;

namespace RepositorySys.Controllers
{
    [CustomActionFilte]
    public class WorkFlow_InstanceStepController : Controller
    {
        //工作流步骤
        IWorkFlow_InstanceStepBLL _workFlow_InstanceBLL;
        IWorkFlow_ModelBLL _workFlow_ModelBLL;

        //用户接口
        IUserBLL _userBLL;

        //耗材信息接口
        IConsumableInfoBLL _consumableInfoBLL;
        public WorkFlow_InstanceStepController(IWorkFlow_InstanceStepBLL workFlow_InstanceBLL, IUserBLL userBLL, IConsumableInfoBLL consumableInfoBL, IWorkFlow_ModelBLL iL)
        {
            _workFlow_InstanceBLL = workFlow_InstanceBLL;
            _userBLL = userBLL;
            _consumableInfoBLL = consumableInfoBL;
            _workFlow_ModelBLL = iL;
        }

        #region 视图

        /// <summary>
        /// 申请 人列表
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public IActionResult AddWorkFlow_InstanceStepView()
        {
            return View();
        }

        /// <summary>
        /// 修改视图
        /// </summary>
        /// <returns></returns>
        public IActionResult UpdataWorkFlow_InstanceStepView()
        {
            return View();
        }

        /// <summary>
        /// 日志视图
        /// </summary>
        /// <returns></returns>
        public IActionResult LogView()
        {
            return View();
        }
        #endregion

        /// <summary>
        /// 获取实例日志信息
        /// </summary>
        /// <param name="InstanceId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getLogList(string InstanceId)
        {
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), _workFlow_InstanceBLL.getLogList(InstanceId)));
        }

        /// <summary>
        /// 步骤条 
        /// </summary>
        /// <param name="InstanceId">实例id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getStepList(string InstanceId)
        {
           object workFlow_Instance = null;
            try
            {
                workFlow_Instance = _workFlow_InstanceBLL.geStepList(InstanceId);
            }
            catch (MyException e)
            {
                //获取异常信息
                return getException(workFlow_Instance, e);
            }
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS),  workFlow_Instance));
        }



        /// <summary>
        /// 申请 人列表、搜索
        /// </summary>
        /// <param name="workFlow_InstanceStepName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getWorkFlow_InstanceStepList(string workFlow_InstanceStepName, string page, string limit)
        {
            List<WorkFlow_InstanceStepDTO> workFlow_Instance = null;
            int cont = 0;
            try
            {
                string userId = HttpContext.Session.GetString("userId");
                workFlow_Instance = _workFlow_InstanceBLL.getworkFlow_InstanceStepList(userId, workFlow_InstanceStepName, page, limit, out cont);
            }
            catch (MyException e)
            {
                //获取异常信息
                return getException(workFlow_Instance, e);
            }
            return Json(new JsonResults(0, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), cont, workFlow_Instance));
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="reviewReason">审核信息</param>
        /// <param name="reviewStatusb">审核状态</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdataWorkFlow_InstanceStep(string id, string reviewReason,int num, WorkFlow_InstanceEnums.WorkFlow_InstanceStepEnums reviewStatusb)
        {
            object data = null;
            try
            {
                //获取当前登录人id
                string userId=  HttpContext.Session.GetString("userId");
                if (string.IsNullOrEmpty(userId))
                {
                    throw new MyException(909,"登录已过期");
                }
                data = _workFlow_InstanceBLL.UpdataWorkFlow_InstanceStep(id, reviewReason, userId, num, reviewStatusb);
            }
            catch (MyException e)
            {
                return getException(data, e);
            }
            return Json(new JsonResults(200, "审核成功", data));
        }
        /// <summary>
        /// 参数验证
        /// </summary>
        /// <param name="department">参数</param>
        /// <returns></returns>
        private bool Verify(DepartmentInfo department, out string mgs)
        {
            //Regex re = new Regex(Regular.Name);
            //if (!re.Match(department.DepartmentName).Success)
            //{
            //    mgs = "请输入正确的部门名称";
            //    return false;
            //}
            //else if (department.LeaderId.EndsWith("0"))
            //{
            //    mgs = "请选择主管";
            //    return false;
            //}
            //else if (department.ParentId != null ? string.IsNullOrEmpty(department.ParentId) : false)
            //{
            //    mgs = "请选择父部门";
            //    return false;
            //}
            //else if (department.Description != null ? !new Regex(Regular.Description).Match(department.Description).Success : true)
            //{
            //    mgs = "请输入 2-36 内的字符的描述信息";
            //    return false;
            //}
            mgs = null;
            return true;
        }
        /// <summary>
        /// 获取异常
        /// </summary>
        /// <param name="data"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private IActionResult getException(object data, MyException e)
        {
            var tt = e.GetExceptionResult();
            return Json(new JsonResults(tt.resultStatus, tt.resultMsg, data));
        }

        /// <summary>
        /// 步骤id
        /// </summary>
        /// <param name="instanceSteID"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult getUpdataWorkFlow_InstanceSteptList(string instanceSteID)
        {
            return Json(new JsonResults(200, Enums.getDescribe(SettingEnums.Json_STATE.SUCCESS), _workFlow_InstanceBLL.getUpdataWorkFlow_InstanceSteptList(instanceSteID)));
        }

    }
}
